import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {

    static void testReplaceStrWithRegex() {
        String template = "Hello, ${name}! You are learning ${lang}!";
        //传入一个Map, 替换成Value
        Map<String, String> map = new HashMap<>();
        map.put("name", "Bob");
        map.put("lang", "Java");
        System.out.println(replaceStrWithRegex(template, map));
    }

    /*
    根据字符串模板，正则表达式与Map返回替换后的字符串
     */
    static String replaceStrWithRegex(String template, Map<String, String> map) {
        Pattern pattern = Pattern.compile("\\$\\{([a-z]{4})}");
        Matcher matcher = pattern.matcher(template);
        StringBuilder sb = new StringBuilder();
        while(matcher.find()) {
            String key = matcher.group(1);
            matcher.appendReplacement(sb, map.get(key));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    static void find3LetterWord() {
        String s = "the quick brown fox jumps over the lazy dog.";
        Pattern p = Pattern.compile("\\wo\\w");
        Matcher matcher = p.matcher(s);
        while(matcher.find()) {
            String sub = s.substring(matcher.start(), matcher.end());
            System.out.println(sub);
        }
    }

    static void replace3LetterWord() {
        String s1 = "the quick brown fox jumps over the lazy dog.";
        String output1 = s1.replaceAll("\\s([a-z]{4})\\s", " <b>$1<b> ");
        System.out.println(output1);
    }

    static boolean isMatchYear(String year) {
        String regex = "20\\d\\d";
        return year.matches(regex);
    }

    static boolean isValidMobile(String mobile) {
        return mobile.matches("^1[3-9]\\d{9}$");
    }

    static void isValidPhone() {
        String re = "^0\\d{2,3}-\\d{7,8}";
        for (String s : List.of("010-12345678", "020-9999999", "0755-7654321")) {
            if (!s.matches(re)) {
                System.out.println("测试失败: " + s);
                return;
            }
        }
        for (String s : List.of("010 12345678", "A20-9999999", "0755-7654.321")) {
            if (s.matches(re)) {
                System.out.println("测试失败: " + s);
                return;
            }
        }
        System.out.println("测试成功!");
    }

    static void test() {
        String re = "learn\\s([jJ]ava|[pP]hp|[gG]o)";
        System.out.println("learn java".matches(re));
        System.out.println("learn Java".matches(re));
        System.out.println("learn php".matches(re));
        System.out.println("learn Go".matches(re));
    }

    /*
    利用分组匹配，从字符串"23:01:59"提取时、分、秒。
     */
    static void pickHMS(String time) {
        if (time == null) {
            throw new IllegalArgumentException();
        }
        String regex = "(\\d{2}):(\\d{2}):(\\d{2})";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(time);
        if (matcher.matches()) {
            System.out.println("Hour: " + matcher.group(1));
            System.out.println("Minute: " + matcher.group(2));
            System.out.println("Second: " +  matcher.group(3));
        }
    }

    /*
    "123000"：3个0
    "10100"：2个0
    "1001"：0个0
     */
    static void countZero() {
        Pattern pattern = Pattern.compile("(\\d+?)(0*)");
        Matcher matcher = pattern.matcher("123000");
        if (matcher.matches()) {
            System.out.println(matcher.group(1));
            System.out.println(matcher.group(2));
        }
    }
}
